package com.crack.nga2.volley;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.utils.GlobalConst;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 上传附件
 * Created by Administrator on 2016/1/7.
 */
public class PostUploadRequest extends Request<Map<String, String>> {
    private UploadAttachDataList list;
    private Response.Listener<Map<String, String>> listener;

    public PostUploadRequest(int method, String url, UploadAttachDataList list, Response.Listener<Map<String, String>> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = listener;
        this.list = list;
        setRetryPolicy(new DefaultRetryPolicy(20000, 0, 1));
    }

    public PostUploadRequest(String url, UploadAttachDataList list, Response.Listener<Map<String, String>> listener, Response.ErrorListener errorListener) {
        this(Method.POST, url, list, listener, errorListener);
    }

    public PostUploadRequest(String url, UploadAttachDataList list, Response.Listener<Map<String, String>> listener) {
        this(url, list, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return list.toArray();
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "multipart/form-data;boundary=" + UploadAttachDataList.BOUNDARY);
        headers.put("Cookie", GlobalConst.Cookie);
        return headers;
    }

    @Override
    protected Response<Map<String, String>> parseNetworkResponse(NetworkResponse response) {
        String json;
        int index;
        try {
            json = new String(response.data, "GBK");
            index = json.indexOf("{");
            if (index == -1) {
                return Response.error(new VolleyError("返回数据异常"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError("返回数据异常"));
        }
        Map<String, String> map = new HashMap<>();
        json = json.substring(index);
        try {
            JSONObject dataObject = new JSONObject(json);
            JSONObject object = dataObject.getJSONObject("data");
            map.put("attachments", object.getString("attachments"));
            map.put("attachments_check", object.getString("attachments_check"));
            map.put("url", object.getString("url"));
            map.put("thumb", object.getString("thumb"));
        } catch (JSONException e) {
            e.printStackTrace();
            Log.i("upload", json);
            return Response.error(new VolleyError("返回数据解析时异常"));
        }
        return Response.success(map, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(Map<String, String> response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }
}
